package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;

/**
 * 活动参与者实体类
 * 记录用户参与活动的报名信息
 */
@Getter
@Setter
@ToString(exclude = {"activity"})
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "activity_participants", indexes = {
        @Index(name = "idx_user_activity", columnList = "user_id,activity_id"),
        @Index(name = "idx_signup_time", columnList = "signup_time")
})
public class ActivityParticipantEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "user_id", nullable = false)
    @NotNull(message = "用户ID不能为空")
    private Long userId;

    @Column(name = "activity_id", nullable = false)
    @NotNull(message = "活动ID不能为空")
    private Long activityId;

    @Column(name = "signup_time", nullable = false)
    @NotNull(message = "报名时间不能为空")
    private LocalDateTime signupTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "activity_id", insertable = false, updatable = false)
    private ActivityEntity activity;

    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @Column(name = "remark", length = 500)
    private String remark;

    @PrePersist
    protected void prePersist() {
        if (this.signupTime == null) {
            this.signupTime = LocalDateTime.now();
        }
    }
}
